﻿using ProxyWrapper;
using ProxyWrapper.UserProxy;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserAccount : System.Web.UI.Page
{
    public string Msg = "";
    public string visible = "none";
    public string isExpiryEnable = "disabled";
    public string messagetype = "";
    public string message = "";
    public ProxyWrapper.UserProxy.UserDetails UserObject { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            if (Session["userObject"] == null)
            {
                Response.Redirect("Default.aspx", false);
            }
            else
            {
                UserObject = ((ProxyWrapper.UserProxy.UserDetails)Session["UserObject"]);
                if (UserObject.RoleId == 1)
                    isExpiryEnable = "";
                if (Request.QueryString["removeemail"] != null)
                {
                    new UserServiceWrapper().AddEMailIds(UserObject.UserId.ToString(), UserObject.UserId.ToString(), string.Empty);
                    Response.Redirect("UserAccount.aspx", false);
                }
            }
            if (Request.Form["company"] != null)
            {
                UpdateData();
            }
        }
        catch (Exception ex)
        { }
    }
    private void UpdateData()
    {
        try
        {
            string fname = Request.Form["fname"].ToString();
            string lname = Request.Form["lname"].ToString();
            string phone = Request.Form["phone"].ToString();
            string userId = UserObject.UserId.ToString();
            string username = UserObject.UserName.ToString();
            string password = UserObject.Password.ToString();
            string email = Request.Form["email"].ToString();
            string expiry = UserObject.AccountExpiry.ToString();
            string company = Request.Form["company"].ToString();
            string timeZone = Request.Form["timezone"].ToString();
            string deviceCount = Request.Form["userCount"] != null ? Request.Form["userCount"].ToString() : "0";
            bool isValidForm = true;
            if (string.IsNullOrWhiteSpace(fname))
            {
                isValidForm = false;
                message = "First Name is required";
            }
            else if (string.IsNullOrWhiteSpace(email))
            {
                isValidForm = false;
                message = "Email is required";
            }
            if (!isValidForm)
                messagetype = "E";
            if (isValidForm)
            {
                UserDetails objUser = new UserDetails()
                {
                    UserName = username,
                    UserId = Convert.ToInt32(userId),
                    FirstName = fname,
                    LastName = lname,
                    PhoneNumber = phone,
                    Password = password,
                    EmailId = email,
                    AccountExpiry = Convert.ToDateTime(expiry),
                    Company = company,
                    TimeZone = timeZone

                };
                if (UserObject.RoleId > 0)
                    objUser.DeviceLimit = !string.IsNullOrWhiteSpace(deviceCount) ? Convert.ToInt32(deviceCount) : 0;

                ProxyWrapper.UserServiceWrapper _wrapper = new ProxyWrapper.UserServiceWrapper();
                Msg = _wrapper.UpdateAccounts(objUser);
                //visible = !string.IsNullOrWhiteSpace(Msg) ? "block" : "none";
                if (Msg.Contains("Successfully"))
                {
                    messagetype = "S";

                }
                else
                {
                    messagetype = "E";
                }
                message = Msg;
                Session["userObject"] = new UserServiceWrapper().ValidateLogin(UserObject.UserName, UserObject.Password);
                Response.Redirect("UserAccount.aspx", false);
            }
        }
        catch (Exception ex)
        {
        }
    }
    public string FillTimeZoneControl()
    {
        Dictionary<string, string> timeZone = StaticData.GetTimeZones();
        StringBuilder sb = new StringBuilder();
        string isSelected = "";
        foreach (KeyValuePair<string, string> item in timeZone)
        {
            if (item.Key.Equals(UserObject.TimeZone))
                isSelected = "selected";
            else
                isSelected = "";
            sb.Append(string.Format("<option " + isSelected + " value={0}>{1}</option>", item.Key, item.Value));
        }
        return sb.ToString();
    }
    public string GetMessage()
    {
        string warning = "<div class='warning'><p></p><li>{0}</li><p></p></div>";
        string result = "<div class='success'><p></p><li>{0}</li><p></p></div>";
        if (messagetype == "E")
            return string.Format(warning, message);
        else if (messagetype == "S")
            return string.Format(result, message);
        else
            return "";

    }

    protected string GetEmailIds(string userId)
    {
        StringBuilder sb = new StringBuilder();
        ProxyWrapper.UserServiceWrapper _wrap = new ProxyWrapper.UserServiceWrapper();
        string ids = _wrap.GetEmailIds(userId);
        if (!string.IsNullOrWhiteSpace(ids))
        {
            string[] arrEmail = ids.Split(',');
            for (int i = 0; i < arrEmail.Length; i++)
            {
                sb.Append(arrEmail[i]);
                sb.Append("<br/>");

            }
            sb.Append("<a href='#' onclick='OpenPage(\"EmailIds.aspx?edit&addedOnId=" + userId + "\")'> Edit All</a>");
            sb.Append(" | <a href='UserAccount.aspx?removeemail=" + userId + ")'> Remove All</a>");
            sb.Append("<br/>");
            return sb.ToString();
        }
        else
        {
            return "No Email Id found. <a href='#' onclick='OpenPage(\"EmailIds.aspx?add&addedOnId=" + userId + "\")'> Add Email Ids</a> <br/>";

        }
    }

}